//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "event\fn_eventObjectiveDestroy.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (!isServer) exitWith{};
private ["_params","_action_id","_object","_damage","_causedBy","_ammunition","_ammo_string","_action","_parameters"
		,"_destroyed_msg","_shared_variable","_shared_variable_value","_ammo_amount","_ammo_types","_destroyed_code", "_damaged"
		,"_damage_value","_instance_id"];
DEFAULT_PARAM(_params,0,[]);
DEFAULT_PARAM(_action_id,1,-1);

if (_action_id < 0) exitWith {ERROR_1("Invalid Parameters",_this)};

_object = GETVAL(_params,0,objNull);
_damage = GETVAL(_params,2,0);
_causedBy = GETVAL(_params,3,objNull);
_ammunition = GETVAL(_params,4,"");
_damage_value = 0;

_action = [_object,_action_id] call FFUNC(actionGet);
TRACE_1("Action",_action);

_parameters = [_action] call FFUNC(actionGetParams);

_destroyed_msg = [_parameters, "DESTROY_MSG", "STR_TMF_TARGET_DESTROYED",THIS_FILE] call FFUNC(hashGet);
if (typename _destroyed_msg == typename "") then {_destroyed_msg = [_destroyed_msg];};

_shared_variable = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
_shared_variable_value = [_parameters, "SHARED_VAR_VAL", true,THIS_FILE] call FFUNC(hashGet);
_ammo_amount = [_parameters, "AMMO_COUNT", 2,THIS_FILE] call FFUNC(hashGet);
_ammo_types = [_parameters, "AMMO_TYPES", (["DEFAULT_EXPLOSIVES",["SatchelCharge_Remote_Mag","DemoCharge_Remote_Mag"]] call FFUNC(getFrameworkValue)),THIS_FILE] call FFUNC(hashGet);
_destroyed_code = [_parameters, "DESTROYED_CODE", "",THIS_FILE] call FFUNC(hashGet);
_shared_variable = _shared_variable call FFUNC(getStringVar);

_ammo_string = _ammunition;
if (typename _ammo_string != typename "") then { _ammo_string = str(_ammunition); };
_debug = [typeof _object,_causedBy,_ammunition, _damage, _destroyed_msg, _shared_variable, _shared_variable_value, _ammo_amount, _ammo_types,(_ammo_types find _ammunition), (_destroyed_code != "")];
DEBUG_1("Params",_debug);

if (IS_STRING(_ammo_types)) then {
	if (_ammo_types == "") then {
		_ammo_types = [];
	} else {
		_ammo_types = [_ammo_types];
	};
};

_damaged = false;
if (((_ammo_types find _ammo_string) >= 0) OR ((_ammo_types find _ammunition) >= 0)) then {
	// We need sufficient damage to take it into account.
	If (_damage > 0.2) then { _damaged = true; };
} else {
	DEBUG_1("Ammo String",ARR_2(_ammo_string,(_ammo_string == str(""))));
	{
		if ((_ammo_string isKindOf _x) || (_ammunition isKindOf _x)) exitWith {
			DEBUG_1("IsKindOf",_x);
			_damaged = true;
		};
	} foreach _ammo_types;
	if (!_damaged) then {
		// Apparently with small moveable items, the ammunition parameter doesn't go through. So we just accept it as is. It's only a workaround... not perfect but... eh...
		If ((_ammo_string == str("")) AND (_causedBy == _object)) then {
			_damaged = true;
			DEBUG_1("Small items damage",ARR_2(_ammo_string,(_causedBy == _object)));
		};
	};
};

If (_damaged) then {
	_ammo_amount = _ammo_amount - 1;
	_parameters = [_parameters, "AMMO_COUNT", _ammo_amount] call FFUNC(hashSet);
	_action = [_action,_parameters] call FFUNC(actionSetParams);
	[_object, _action] call FFUNC(actionSet);
	If (_ammo_amount <= 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		_event = [_action] call FFUNC(actionGetEvent);
		_object removeEventHandler ["HandleDamage", _event];
		_object allowDamage True;
		_object setDamage 1.1;
		If (count _destroyed_msg > 0) then { [QGVAR(all_message),[_destroyed_msg,"all",objNull,"hq"],THIS_FILE] call FFUNC(callEvent); };
		if (!isNil "_shared_variable") then {
			if (_shared_variable != "") then {
				[_instance_id,_shared_variable,_shared_variable_value] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_variable,_shared_variable_value],THIS_FILE] call FFUNC(callEvent);
			};
		};
		if (_destroyed_code != "") then {
			[QGVAR(all_execute),[_destroyed_code,_object],THIS_FILE] call FFUNC(callEvent);
		};
		// For small non destructible objects, we need to delete them ourselves.
		If ((_ammo_string == str("")) AND (_causedBy == _object)) then {
			deleteVehicle _object;
		};
		DEBUG_1("Destroyed",ARR_3(typeof _object,_shared_variable, _shared_variable_value));
	} else {
		DEBUG_1("Damaged",ARR_3(typeof _object,_ammo_amount));
	};
	_damage_value = 0.3;
};

TRACE("End");
TMFPROFILERSTOP;
_damage_value;
